package com.scarecrow.compack.component.spatial {
	import com.scarecrow.compack.family.ISpatialComponent;
	import com.scarecrow.olga.game.component.GameComponent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class SpatialComponent extends GameComponent implements ISpatialComponent {
		
		public static const PROP_POSITION:String = "pos";
		public static const PROP_DIMENSION:String = "dims";
		
		private var _orientation:String;
		private var _area:Rectangle;
		private var ownerPosition:Point;
		
		public function SpatialComponent(xPos:int, yPos:int, w:int, h:int) {
			_area = new Rectangle(xPos, yPos, w, h);
		}
		
		override protected function initialize():void {
			owner.setProp(PROP_POSITION, new Point(_area.x, _area.y));
			ownerPosition = owner.getProp(PROP_POSITION);
		}
		
		/* INTERFACE com.scarecrow.compack.family.ISpatialComponent */
		
		public function get x():int {
			return _area.x;
		}
		
		public function set x(value:int):void {
			if (!ownerPosition) return;
			_area.x = value;
			ownerPosition.x = value;
		}
		
		public function get y():int {
			return _area.y;
		}
		
		public function set y(value:int):void {
			if (!ownerPosition) return;
			_area.y = value;
			ownerPosition.y = value;
		}
		
		public function get width():int {
			return _area.width;
		}
		
		public function set width(value:int):void {
			_area.width = value;
		}
		
		public function get height():int {
			return _area.height;
		}
		
		public function set height(value:int):void {
			_area.height = value;
		}
		
		public function get area():Rectangle {
			return _area;
		}
		
		public function get orientation():String {
			return _orientation;
		}
		
		public function set orientation(value:String):void {
			_orientation = value;
		}
		
		public function collidesWith(spatialComponent:SpatialComponent):Boolean {
			return area.intersects(spatialComponent.area);
		}
	}

}